﻿using AutoMapper;
using B.S.Filtration.Domain;
using B.S.Filtration.ErrorCode;
using B.S.Filtration.Write.Api.Application.Command.Cleaning;
using MediatR;
using SqlSugar;

namespace B.S.Filtration.Write.Api.Application.CommandHander
{
    public class DeleteCleaningHander : IRequestHandler<DeleteCleaningCommand, APIResult<int>>
    {

        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        public DeleteCleaningHander(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        /// <summary>
        /// 删除设备清洗
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<APIResult<int>> Handle(DeleteCleaningCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var list = db.Queryable<Domain.Cleaning>().Where(x => x.CleaningId == request.CleaningId).First();

            var model = mapper.Map<Domain.Cleaning>(list);

            var yes = db.Deleteable<Domain.Cleaning>(model).ExecuteCommandAsync();
            if (yes != null)
            {
                result.code = APIResultCode.success;
                result.msg = "物料备料删除成功";
            }
            else
            {
                result.code = APIResultCode.failed;
                result.msg = "物料备料删除失败";
            }
            return Task.FromResult(result);
        }
    }
}
